/**
 * Created by WYH on 17/4/17.
 */
angular.module('tribalc').service('StaffModel', ['Enum', 'Check', 'Prompt','Alert' ,function (Enum, Check, Prompt,Alert) {
    var StaffModel = function (object) {

        var self = this;
        init();

        function init() {
            self.id = object ? object.id : null;
            self.name = object ? object.name : null;
            self.sex = object ? object.sex ? object.sex : 'UNKNOWN' : 'UNKNOWN';
            self.idNo = object ? object.idNo : null;
            self.phone = object ? object.phone : null;
            self.department = object ? object.department : null;
            self.position = object ? object.position : null;
            self.doorCardNum = object ? object.doorCardNum : null;
            self.staffBindStatus = object ? object.staffBindStatus : null;
            self.roles = object ? object.roles : [];

        }
    };

    StaffModel.prototype.getSex = function () {
        return Enum.Sex[this.sex] || Enum.Sex['UNKNOWN'];
    };

    StaffModel.prototype.setSex = function (sex) {
        return Enum.SexFormat[sex || Enum.Sex.UNKNOWN];
    };

    StaffModel.prototype.getBindStatus = function () {
        if (this.roles != null && this.roles.length > 0 && this.roles[0] == "AGENT")
            return '已设代理人';
        else
            return Enum.BindStatus[this.staffBindStatus] || '未绑定';

    };

    StaffModel.prototype.setBindStatus = function (status) {
        return Enum.BindStatusFormat[status || Enum.BindStatus.TO_BIND];
    };

    StaffModel.prototype.getObject = function () {
        return {
            name: this.name,
            sex: this.sex,
            idNo: this.idNo,
            phone: this.phone,
            department: this.department,
            position: this.position,
            doorCardNum: this.doorCardNum,
            roles:this.roles
        }
    };
  StaffModel.prototype.getAddObject = function () {
    return {
      name: this.name,
      sex: this.sex,
      idNo: this.idNo,
      phone: this.phone,
      department: this.department,
      position: this.position,
      // doorCardNum: this.doorCardNum,
      roles: this.roles
    }
  };
  
  StaffModel.prototype.isValid = function () {
    var nameCondition = Check.staffName(this.name);
    var idNoCondition = Check.idNo(this.idNo);
    var phontCondition = Check.mobilePhone(this.phone);
    return check(nameCondition, '员工姓名错误，' + Prompt.staffNameError)
      && check(idNoCondition, Prompt.idNumberError)
      && check(phontCondition, Prompt.mobilePhoneError)
    
  };

    function check(condition, message) {
        if (!condition) {
            Alert.alertBox(message);
            return false;
        }
        return true;
    }

    StaffModel.setupStaffList = function (array) {
        var staffList = [];
        array.forEach(function (value) {
            var staff = new StaffModel(value);
            staffList.push(staff);
        });
        return staffList;
    };

    return StaffModel;

}]);